/*  歌曲对象  */
import Singer from './singer'

export default class Song {
  constructor ({id, name, singer, duration, image, url}) {
    this.id = id //  ID
    this.name = name //  名称
    this.singer = singer //  歌手
    this.duration = duration //  时长
    this.image = image //  封面
    this.url = url //  播放地址
  }

  getLyric () {

  }
}

export function createSong (musicData) {
  return new Song({
    id: musicData.id, // 歌曲id
    name: musicData.name, // 歌曲名称
    singer: filterSinger(musicData), // 歌手
    duration: filterDuration(musicData), // 歌曲时长  毫秒
    image: filterImgUrl(musicData), // 歌曲图片
    url: musicData.url // 歌曲链接 1
  })
}

function filterSinger (musicData) {
  if (musicData.ar) {
    return musicData.ar.map(item => new Singer({
      id: item.id,
      name: item.name,
      img: ''
    }))
  }
  if (musicData.artists) {
    return musicData.artists.map(item => new Singer({
      id: item.id,
      name: item.name,
      img: item.img1v1Url || ''
    }))
  }
}

function filterDuration (musicData) {
  return musicData.dt ? parseInt(musicData.dt / 1000) : parseInt(musicData.duration / 1000)
}

function filterImgUrl (musicData) {
  return musicData.al ? musicData.al.picUrl : musicData.artists[0].img1v1Url
}
